class Solution {
public:
    // FIXME: You can briefly describe the purpose of the function
    int calPoints(vector<string>& ops) {
        
        stack<int> res;

        for(int i = 0; i < ops.size(); i++)
        {
            if(ops[i] == "C")
            {
                res.pop();
                continue;
            }
            else if(ops[i] == "D")
            {
                res.push(2 * res.top());
                continue;
            }
            else if(ops[i] == "+")
            {
                int a = res.top();
                res.pop();
                int b = res.top();
                res.push(a);
                res.push(a + b);
            }
            else res.push(stoi(ops[i]));
        }

        int sum = 0;

        while(!res.empty())
        {
            sum += res.top();
            res.pop();
        }
        
        return sum;
    }
};
